package com.cavy.platform.server.repository.impl;

import com.cavy.platform.server.domain.PluginRepositorySetting;
import com.cavy.platform.server.repository.PluginRepositorySettingRepository;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;


/**
 * Created by tangxu on 18-7-11.
 */
@Repository
public class PluginRepositorySettingRepositoryImpl implements PluginRepositorySettingRepository {
    public static final String PLUGIN_REPOSITORY_FILE_NAME = "repository.json";
    private PluginRepositorySettingList settingList;
    @Value("${cavy.plugin.dir}")
    private String pluginDir;

    @Override
    public void insert(PluginRepositorySetting convert) throws IOException {
        settingList.getList().add(convert);
        write(settingList);
    }

    private void write(PluginRepositorySettingList settingList) {
        File file = new File(pluginDir, PLUGIN_REPOSITORY_FILE_NAME);
        File parentFile = file.getParentFile();
        if (!parentFile.exists()) {
            boolean mkdirs = parentFile.mkdirs();
            if (!mkdirs) {
                throw new RuntimeException("创建文件夹" + parentFile.getPath() + "失败");
            }
        }
        PluginRepositorySettingList clone = cloneList(settingList);
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.writeValue(file, clone);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private PluginRepositorySettingList cloneList(PluginRepositorySettingList settingList) {
        PluginRepositorySettingList list = new PluginRepositorySettingList();
        List<PluginRepositorySetting> list1 = list.getList();
        List<PluginRepositorySetting> list2 = settingList.getList();
        for (PluginRepositorySetting pluginRepositorySetting : list2) {
            if (pluginRepositorySetting.isLocal()) {
                continue;
            }
            list1.add(pluginRepositorySetting);
        }
        list.setList(list1);
        return list;
    }

    @Override
    public List<PluginRepositorySetting> getList() {
        loadSettingList();
        PluginRepositorySetting localSetting = LocalPluginRepositoryImpl.getLocalSetting();
        settingList.getList().add(localSetting);
        return settingList.getList();
    }

    @Override
    public long countByName(String name) {
        for (PluginRepositorySetting setting : settingList.getList()) {
            if (setting.getName().equals(name)) {
                return 1;
            }
        }
        return 0;
    }

    @Override
    public long countByCode(String code) {
        for (PluginRepositorySetting setting : settingList.getList()) {
            if (setting.getCode().equalsIgnoreCase(code)) {
                return 1;
            }
        }
        return 0;
    }

    @Override
    public void refresh() {
        loadSettingList();
    }

    @Override
    public void delete(String name) {
        Iterator<PluginRepositorySetting> iterator = settingList.getList().iterator();
        while (iterator.hasNext()) {
            PluginRepositorySetting next = iterator.next();
            if (next.getName().equals(name)) {
                iterator.remove();
            }
        }
        write(settingList);
    }

    @PostConstruct
    private void loadSettingList() {
        File file = new File(pluginDir, PLUGIN_REPOSITORY_FILE_NAME);
        try {
            if (file.exists()) {
                ObjectMapper mapper = new ObjectMapper();
                settingList = mapper.readValue(file, PluginRepositorySettingList.class);
            } else {
                settingList = new PluginRepositorySettingList();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}
